import React, { useEffect, useState } from 'react'
import { Form, Button, Popconfirm, Modal, DatePicker, Row, Col,Input } from 'antd'
import { connect } from 'umi'

const FormItem = Form.Item
const { TextArea } = Input

const formLayout = {
  labelCol: { span: 7 },
};

const CreateForm = (props) => {
  const { onSubmit, onCancel,record, visible,loading } = props;
  const [form] = Form.useForm();

  useEffect(() => {
   if(record){
     form.setFieldsValue(record);
   }
  }, [visible]);

  const onFormCancel = () => {
    form.resetFields();
    onCancel();
  };

  const handleSubmit = async () => {
    const fieldsValue = await form.validateFields();
    form.resetFields();
    onSubmit({
      ...fieldsValue,
      status:3,
      id:record.id,
    })
  };

  const renderContent = () => {
    return (
      <>
        <Row gutter={24}>
          <Col span={20}>
            <FormItem name="name" label="申请人姓名">
              <Input disabled={true} />
            </FormItem>
          </Col>
          <Col span={20}>
            <FormItem name="remark" label="驳回原因" rules={[{ required: true, message: '请输入驳回原因！' }]}>
              <TextArea rows={4} />
            </FormItem>
          </Col>
        </Row>
      </>
    )
  }

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => onFormCancel()}>取消</Button>
        <Popconfirm title={`是否确认驳回?`}
                    onConfirm={handleSubmit}
                    okText="是"
                    cancelText="否">
          <Button type="primary" loading={loading}>确认</Button>
        </Popconfirm>
      </>
    )
  }

  return (
    <Modal
      width={1024}
      destroyOnClose
      title="驳回修改"
      visible={visible}
      footer={renderFooter()}
      onOk={handleSubmit}
      onCancel={() => onFormCancel()}
      onClose={() => onFormCancel()}
      okText="确认"
      cancelText="取消"
    >
      <Form
        {...formLayout}
        form={form}
        style={{ paddingTop: '24px' }}
      >
        {renderContent()}
      </Form>
    </Modal>
  )
}


export default connect(() => ({
}))(CreateForm)
